<?php
namespace app\mini\controller\v1\client;

use app\common\library\exception\ApiException;
use app\common\library\MemberVo;
use app\common\logic\MemberLogic;
use app\common\model\Agent;
use app\mini\controller\Common;
use think\Request;
use think\response\Json;

class Member extends Common
{

    public $MemberLogic;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->MemberLogic = new MemberLogic();
    }

    /**
     * 我的信息
     * @return \think\response\Json
     */
    public function info(): Json
    {
        $res = $this->exception(function (){
            return $this->MemberLogic->info();
        });
        return $this->response($res);
    }

    /**
     * 修改姓名
     * @return Json
     */
    public function member_name() : Json
    {
        $res = $this->exception(function (){
            return $this->MemberLogic->member_name(request()->param());
        });
        return $this->response($res);
    }

    /**
     * 设置生日
     * @return Json
     */
    public function birthday() : Json
    {
        $res = $this->exception(function (){
            return $this->MemberLogic->birthday(request()->param());
        });
        return $this->response($res);
    }

    /**
     * 设置性别
     * @return Json
     */
    public function sex() : Json
    {
        $res = $this->exception(function (){
            return $this->MemberLogic->sex(request()->param());
        });
        return $this->response($res);
    }

    /**
     * 设置头像
     * @return Json
     */
    public function head() : Json
    {
        $res = $this->exception(function (){
            return $this->MemberLogic->head(request()->param());
        });
        return $this->response($res);
    }

    /**
     * 获取余额
     * @return array
     */
    public function memberBalance():Json
    {
        $res = $this->exception(function (){
            return $this->MemberLogic->balance();
        });
        return $this->response($res);
    }


    /**
     * 绑定代理
     * @return Json
     */
    public function bind_agent()
    {
        $res = $this->exception(function (){
            $params = request()->param();
            $info = Agent::get(['code'=>$params['code']]);
            if (empty($info))
                throw new ApiException('未找到相关代理信息',404);
            if (!empty($info['member_id'])){
                if ($info['member_id'] != MemberVo::instance()->getMemberId())
                    throw new ApiException('该代理已被其他用户绑定',404);
                throw new ApiException('您已绑定了该代理，请勿重复绑定',404);
            }
            $info->member_id = MemberVo::instance()->getMemberId();
            $info->save();
            return true;
        });
        return $this->response($res);
    }

    /**
     * 解除代理绑定
     * @return Json
     */
    public function remove_bind_agent()
    {
        $res = $this->exception(function (){
            $params = request()->param();
            $info = Agent::get(['code'=>$params['code']]);
            if (empty($info))
                throw new ApiException('未找到相关代理信息',404);
            if (!empty($info['member_id'])){
                if ($info['member_id'] != MemberVo::instance()->getMemberId())
                    throw new ApiException('该代理已被其他用户绑定，您无权解除',404);
                $info->member_id = '';
                $info->save();
                return true;
            }
            throw new ApiException('该代理未被其他用户绑定，无需解除',404);
        });
        return $this->response($res);
    }



}